package com.vhall.component.controller.vote;

import com.vhall.component.controller.vote.facade.VoteRoomFacade;
import com.vhall.component.entity.page.vo.PageVO;
import com.vhall.component.entity.vote.vo.*;
import com.vhall.component.entity.vote.vo.room.*;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.plugin.common.annotation.TokenAuth;
import com.vhall.component.plugin.common.annotation.VssTokenAuth;
import com.vhall.component.plugin.common.support.Token;
import com.vhall.component.plugin.common.support.TokenThreadLocal;
import com.vhall.component.plugin.common.support.VssToken;
import com.vhall.component.plugin.common.support.VssTokenThreadLocal;
import com.vhall.component.service.vote.VoteRoomService;
import com.vhall.component.service.vote.VoteService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


/**
 * 直播间内投票
 *
 * @author longzhen.gong
 * @date 2021/7/7 10:51
 */

@VhallComponent

public class VoteRoomController implements VoteRoomFacade {


    @Autowired
    private VoteService voteService;

    @Autowired
    private VoteRoomService voteRoomService;

    @VssTokenAuth
    @Override
    public VotesCreateRspVO create(VotesCreateRoomReqVO reqVO) {
        VssToken vssToken = VssTokenThreadLocal.getToken();
        reqVO.setAccountId(vssToken.getThirdPartyUserId());
        VotesCreateReqVO votesCreateReqVO = new VotesCreateReqVO();
        BeanUtils.copyProperties(reqVO, votesCreateReqVO);
        votesCreateReqVO.setLimitTime(Integer.valueOf(reqVO.getLimitTime()));
        return voteService.create(votesCreateReqVO);

    }

    @VssTokenAuth
    @Override
    public VotesUpdateRspVO update(VotesUpdateRoomRepVO repVO) {
        VotesUpdateRepVO votesUpdateRepVO = new VotesUpdateRepVO();
        BeanUtils.copyProperties(repVO, votesUpdateRepVO);
        votesUpdateRepVO.setLimitTime(Integer.valueOf(repVO.getLimitTime()));
        VssToken vssToken = VssTokenThreadLocal.getToken();
        votesUpdateRepVO.setAccountId(vssToken.getThirdPartyUserId());
        return voteService.update(votesUpdateRepVO);
    }
    @TokenAuth
    @Override
    public PageVO<VotesListRspVO> list(VotesListRoomReqVO repVO) {
        Token token = TokenThreadLocal.getToken();

        VotesListReqVO votesListReqVO = new VotesListReqVO();
        BeanUtils.copyProperties(repVO, votesListReqVO);
        votesListReqVO.setAccountId(token.getAccountId() + "");
        return voteService.list(votesListReqVO);
    }

    @VssTokenAuth
    @Override
    public Boolean delete(VoteDeleteReq req) {
        VssToken token = VssTokenThreadLocal.getToken();
        Integer accountId = token.getThirdPartyUserId();
        return voteRoomService.delete(req.getVoteId(), accountId);
    }

    @VssTokenAuth
    @Override
    public VoteInfoRspVO info(VoteInfoReqVO reqVO) {
        VssToken vssToken = VssTokenThreadLocal.getToken();
        return voteService.info(reqVO, vssToken.getThirdPartyUserId());
    }
    @VssTokenAuth
    @Override
    public List<VoteDetailRspVO> voteDetail(VoteDetailReqVO reqVO) {
        VssToken vssToken = VssTokenThreadLocal.getToken();
        return voteService.voteDetail(reqVO, vssToken.getThirdPartyUserId());
    }

    /**
     * 绑定投票
     *
     * @param reqVO
     * @return
     */
    @VssTokenAuth
    @Override
    public Boolean bindRoom(BindRoomReqVO reqVO) {
        return voteService.bindRoom(Long.valueOf(reqVO.getVoteId()), reqVO.getRoomId());
    }
    @VssTokenAuth
    @Override
    public Boolean unBindRoom(UnBindRoomReqVO reqVO) {
        return voteRoomService.unBindRoom(Long.valueOf(reqVO.getVoteId()), reqVO.getRoomId());
    }

    @VssTokenAuth
    @Override
    public CopyRspVO copy(CopyReqVO reqVO) {
        VssToken vssToken = VssTokenThreadLocal.getToken();
        Integer accountId = vssToken.getThirdPartyUserId();
        return voteRoomService.copy(reqVO, accountId);
    }

    /**
     * 观看端回答投票
     *
     * @param reqVO
     * @return
     */
    @VssTokenAuth
    @Override
    public Boolean answer(AnswerReqVO reqVO) {
        VssToken vssToken = VssTokenThreadLocal.getToken();
        Integer accountId = vssToken.getThirdPartyUserId();
        reqVO.setAccountId(accountId + "");
        return voteRoomService.answer(reqVO);
    }

    /**
     * 发布投票 主播端
     *
     * @param reqVO
     * @return
     */
    @VssTokenAuth
    @Override
    public Boolean publish(PublishReqVO reqVO) {
        return voteRoomService.publish(reqVO);
    }

    /**
     * 取消投票发布
     *
     * @param reqVO
     * @return
     */
    @VssTokenAuth
    @Override
    public Boolean cancelPublish(PublishReqVO reqVO) {

        return voteRoomService.cancelPublish(reqVO);
    }

    /**
     * 投票结束
     *
     * @param reqVO
     * @return
     */
    @VssTokenAuth
    @Override
    public Boolean voteFinish(FinishReqVO reqVO) {
        return voteRoomService.voteFinish(reqVO);
    }

    /**
     * 投票结束后，投票公布结果
     *
     * @param reqVO
     * @return
     */
    @VssTokenAuth
    @Override
    public Boolean votePush(PushReqVO reqVO) {
        return voteRoomService.votePush(reqVO);
    }

    /**
     * 投票中，投票公布结果  投票实时统计结果推送(不发公告)
     *
     * @param reqVO
     * @return
     */
    @VssTokenAuth
    @Override
    public Boolean pushStatis(PushStatisReqVO reqVO) {

        return voteRoomService.pushStatis(reqVO);
    }

    /**
     * 观众端调用 是否提交过投票
     *
     * @param reqVO
     * @return
     */
    @VssTokenAuth
    @Override
    public CheckSurveyRspVO checkSurvey(CheckSurveyReqVO reqVO) {
        return voteRoomService.checkSurvey(reqVO);
    }
    @VssTokenAuth
    @Override
    public VotesListRspVO getLastVote(@RequestParam("room_id") String roomId) {
        return null;
    }


}
